package Hangman;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

/**
 * FileLoader class
 * Used for saving and loading the game settings and reading/writing from files.
 * @author Robin E, Jakob L, Isac A, Cristian O, Eric P, Tobias W. 
 *
 */

public class FileLoader {
	
	private String selectedTheme = "Movies";
	private ArrayList<String> wordList;
	private String difficulty = "Easy";
	private String playersAmount = "1";
	
	/**
	 * FileLoader() is just a simple constructor of the class FileLoader
	 */
	public FileLoader()
	{}
	
	/**
	 * getDifficulty(), used to return the game difficulty
	 * @return difficulty
	 */
	public String getDifficulty() {
		return difficulty;
	}
	/**
	 * setDifficulty(String difficulty) is used to set the game difficulty
	 * @param difficulty which difficulty the game should have
	 */
	public void setDifficulty(String difficulty) {
		this.difficulty = difficulty;
	}
	/**
	 * getPlayerAmount() is used to return the number of players in the game
	 * @return playersAmount
	 */
	public String getPlayersAmount() {
		return playersAmount;
	}
	/**
	 * setPlayersAmount(String playersAmount) is used to set the number of players the game should have
	 * @param playersAmount the number of players
	 */
	public void setPlayersAmount(String playersAmount) {
		this.playersAmount = playersAmount;
	}
	/**
	 * setWordList(ArrayList<String> wordList) is used to set the wordList to be used of the game
	 * @param wordList which list to be used
	 */
	public void setWordList(ArrayList<String> wordList) {
		this.wordList = wordList;
	}
	/**
	 * getTheme() returns the current theme of the game
	 * @return selectedTheme the theme of the game 
	 */
	public String getTheme()
	{
		return selectedTheme;
	}
	/**
	 * writeSettings()
	 * Writes the game settings to the text file for future loading of the settings
	 */
	public void writeSettings()
	{
		try (BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("settingsfolder/settings.txt"))){
			bufferedWriter.write("players="+getPlayersAmount());
			bufferedWriter.write(System.lineSeparator());
			bufferedWriter.write("difficulty="+getDifficulty());
			bufferedWriter.write(System.lineSeparator());
			bufferedWriter.write("theme="+ getTheme());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * readSettings() is used to return the settings from the previously saved text file.
	 * @return the String array with all the settings in the game
	 */
	public String[] readSettings()
	{
		String[] settings=new String[3];
		try (BufferedReader bufferedReader = new BufferedReader(new FileReader("settingsfolder/settings.txt"))){
			for (int i = 0; i < settings.length; i++)
				if (bufferedReader.ready())
					settings[i] = bufferedReader.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return settings;
	}
	/**
	 * updateSettings() is used to update the game settings from the readSettings() method
	 */
	public void updateSettings() {
		/*Creates an array that gets initialize with an
		array returned from fileReader()*/
		String[] currentSettings = readSettings();
		for (int i = 0; i < currentSettings.length; i++) {
			String[] name = null;
			if (currentSettings[i] != null) {
				name = currentSettings[i].split("=");
				switch (name[0]) {
				case "players":
				{
					String playersValue = name[1];
					setPlayersAmount(playersValue);
					break;
				}
				case "difficulty":
				{
					String difficultyValue = name[1];
					setDifficulty(difficultyValue);
					break;
				}
				case "theme":
				{
					String themeValue = name[1];
					setSelectedTheme(themeValue);
					break;
				}
				}
			}
		}
	}
	/**
	 * listReader(String path)
	 * This method reads the content of the text files where the words are.
	 * and adds it to wordList.
	 * @param path the path to text files
	 */
	private void listReader(String path){
		wordList= new ArrayList<>();
		try(BufferedReader bufferedReader= new BufferedReader(new FileReader(path))){
			while(bufferedReader.ready()){
				wordList.add(bufferedReader.readLine());
			}
		}catch(IOException e){
			e.printStackTrace();
			System.out.println("error creating fileReader");
		}
	}
	
	/**
	 * genearateList()
	 * This method calls listReader with the path
	 * to the selected theme.
	 */
	public void generateList(){
		String path= null;
		
		switch(selectedTheme){
		case "Animals":
			path= "wordList/themeAnimals.txt";
			break;
		case "Movies":
			path= "wordList/themeMovies.txt";
			break;
		case "Games":
			path= "wordList/themeGames.txt";
			break;
		}
		listReader(path);
	}
	/**
	 * getRandomWord() is used to return a random word from the word list
	 * @return Word object with a random word 
	 */
	public Word getRandomWord(){
		
		Collections.shuffle(wordList);
		String[] wordS= wordList.get(0).split("");
		
		Letter[] returnedWord = new Letter[wordS.length];
		
		for(int i = 0; i < wordS.length; i++)
		{
			returnedWord[i] = new Letter(wordS[i]);
		}
		
		Word temp = new Word(returnedWord);
				
		return temp;
	}
	
	/**
	 * setSelectedTheme(String theme)
	 * This method sets the theme according to the parameter entered.
	 * @param theme the theme selected by the user.
	 */
	public void setSelectedTheme(String theme) {
		selectedTheme = theme;
	}
	
	/**
	 * getWordList()
	 * This method returns the ArrayList used for theme words.
	 * @return arrayList used for words.
	 */
	public ArrayList<String> getWordList() {
		return wordList;
	}
}
